Ext.define('SKJF.view.PENJUALAN.v_dodetail_form', {
	extend	: 'Ext.form.Panel',
	
	alias	: 'widget.v_dodetail_form',
	
	//region:'east',
	//id: 'east-region-container',
	
	title		: 'Create/Update dodetail',
    bodyPadding	: 5,
    autoScroll	: true,
    
    initComponent: function(){
    	/*
		 * Deklarasi variable setiap field
		 */
		
		var poclient_store = Ext.create('SKJF.store.s_poclient',{autoLoad:true});
		//var barang_store = Ext.create('SKJF.store.s_poclientdet');
		var mstsatuan_store = Ext.create('SKJF.store.s_mstsatuan',{autoLoad:true});
		
		var barang_store = Ext.create('Ext.data.Store', {
			fields: [
                {name: 'NOMOR_PO', type: 'string', mapping: 'NOMOR_PO'},
                {name: 'KODE_BARANG', type: 'string', mapping: 'KODE_BARANG'},
                {name: 'NAMA_BARANG', type: 'string', mapping: 'NAMA_BARANG'},
                {name: 'JENIS_BARANG', type: 'string', mapping: 'JENIS_BARANG'},
                {name: 'JUMLAH', type: 'number', mapping: 'JUMLAH'},
                {name: 'SATUAN', type: 'string', mapping: 'SATUAN'},
                {name: 'HARGA_SATUAN', type: 'string', mapping: 'HARGA_SATUAN'},
                {name: 'TARGET_KIRIM_BRG', type: 'date', mapping: 'TARGET_KIRIM_BRG'},
                {name: 'HSCODE', type: 'string', mapping: 'HSCODE'},
                {name: 'STOCK', type: 'number', mapping: 'STOCK'}
            ],
			proxy: {
				type: 'ajax',
				api: {
					read    : 'c_dodetail/get_barang_from_poclientdet'
					// create	: 'c_mstcustomer/save',
					// update	: 'c_mstcustomer/save',
					// destroy	: 'c_mstcustomer/delete'
				},
				actionMethods: {
					read    : 'POST',
					create	: 'POST',
					update	: 'POST',
					destroy	: 'POST'
				},
				reader: {
					type            : 'json',
					root            : 'data',
					rootProperty    : 'data',
					successProperty : 'success',
					messageProperty : 'message'
				},
				writer: {
					type            : 'json',
					writeAllFields  : true,
					root            : 'data',
					encode          : true
				},
				listeners: {
					exception: function(proxy, response, operation){
						Ext.MessageBox.show({
							title: 'REMOTE EXCEPTION',
							msg: operation.getError(),
							icon: Ext.MessageBox.ERROR,
							buttons: Ext.Msg.OK
						});
					}
				}
			}
			/* proxy: {
				type: 'ajax',
				url: 'c_dodetail/get_barang_from_poclientdet',
				reader: {
					type: 'json',
					root: 'data'
				}
			},
			autoLoad : true */
		});
		
		var NOMOR_DO_field = Ext.create('Ext.form.field.Text', {
			itemId: 'NOMOR_DO_field',
			name: 'NOMOR_DO', /* column name of table */
			fieldLabel: 'NOMOR_DO',
			allowBlank: true,
			readOnly: true
		});
		var TGL_DETAIL_DO_field = Ext.create('Ext.form.field.Date', {
			name: 'TGL_DETAIL_DO', /* column name of table */
			format: 'Y-m-d',
			fieldLabel: 'TGL_DETAIL_DO',
			value: new Date()
		});
		var NOMOR_PO_field = Ext.create('Ext.form.field.ComboBox', {
			itemId: 'NOMOR_PO_field',
			name: 'NOMOR_PO', /* column name of table */
			fieldLabel: 'NOMOR_PO',
			pageSize: 10,
			typeAhead    : true,
			triggerAction: 'all',
			selectOnFocus: true,
            loadingText  : 'Searching...',
			store: poclient_store,
			queryMode: 'local',
			tpl: Ext.create('Ext.XTemplate',
				'<tpl for=".">',
					'<div class="x-boundlist-item">{NOMOR_PO}</div>',
				'</tpl>'
			),
			displayTpl: Ext.create('Ext.XTemplate',
				'<tpl for=".">',
					'{NOMOR_PO}',
				'</tpl>'
			),
			valueField: 'NOMOR_PO',
			displayField: 'NOMOR_PO',
			enableKeyEvents: true,
			listeners: {
				select: function(combo, records, e){
					var po = records[0].data.NOMOR_PO;
					barang_store.clearFilter(true);
					barang_store.load({
						params: {
							NOMOR_PO: po
						}
					});
					// barang_store.filter('NOMOR_PO',records[0].data.NOMOR_PO);
				}
			}
		});
		var KODE_BARANG_field = Ext.create('Ext.form.field.Hidden', {
			name: 'KODE_BARANG', /* column name of table */
			fieldLabel: 'KODE_BARANG',
			readOnly: true
		});
		var SISABARANG_field = Ext.create('Ext.form.field.Number', {
			name: 'SISABARANG', /* column name of table */
			fieldLabel: 'SISA BARANG',
			maxLength: 11,
			readOnly: true
		});
		var SISAPOCDET_field = Ext.create('Ext.form.field.Number', {
			name: 'SISAPOC', /* column name of table */
			fieldLabel: 'SISA POCLIENT',
			maxLength: 11,
			readOnly: true
		});
		var HARGA_SATUAN_field = Ext.create('Ext.form.field.Number', {
			name: 'HARGA_SATUAN', /* column name of table */
			fieldLabel: 'HARGA_SATUAN'
		});
		
		var NAMA_BARANG_field = Ext.create('Ext.form.field.ComboBox', {
			itemId: 'NAMA_BARANG_field',
			name: 'NAMA_BARANG', /* column name of table */
			fieldLabel: 'NAMA_BARANG',
			pageSize: 10,
			typeAhead    : true,
			triggerAction: 'all',
			selectOnFocus: true,
            loadingText  : 'Searching...',
			store: barang_store,
			queryMode: 'local',
			tpl: Ext.create('Ext.XTemplate',
				'<tpl for=".">',
					'<div class="x-boundlist-item">{NAMA_BARANG}</div>',
				'</tpl>'
			),
			displayTpl: Ext.create('Ext.XTemplate',
				'<tpl for=".">',
					'{NAMA_BARANG}',
				'</tpl>'
			),
			valueField: 'NAMA_BARANG',
			displayField: 'NAMA_BARANG',
			enableKeyEvents: true,
			listeners: {
				select: function(combo, records, e){
					Ext.Ajax.request({
						url: 'c_dodetail/getSisa',
						params: {
							NOMOR_PO: records[0].data.NOMOR_PO,
							KODE_BARANG: records[0].data.KODE_BARANG,
							NAMA_BARANG: records[0].data.NAMA_BARANG
						},
						success: function(response){
							var rs = Ext.decode(response.responseText);
							SISABARANG_field.setValue(rs.sisabarang);
							SISAPOCDET_field.setValue(rs.sisabarangpoc);
						}
					});
					KODE_BARANG_field.setValue(records[0].data.KODE_BARANG);
					QUANTITY_field.setValue(records[0].data.JUMLAH);
					QUANTITY_field.setMaxValue(records[0].data.JUMLAH);
					SATUAN_field.setValue(records[0].data.SATUAN);
					HARGA_SATUAN_field.setValue(records[0].data.HARGA_SATUAN);
				}
			}
		});
		var BATCH_NO_field = Ext.create('Ext.form.field.Text', {
			name: 'BATCH_NO', /* column name of table */
			fieldLabel: 'BATCH_NO',
			maxLength: 100,
			allowBlank: false
		});
		var SATUAN_field = Ext.create('Ext.form.field.ComboBox', {
			name: 'SATUAN', /* column name of table */
			fieldLabel: 'SATUAN',
			pageSize: 10,
			typeAhead    : true,
			triggerAction: 'all',
			selectOnFocus: true,
            loadingText  : 'Searching...',
			store: mstsatuan_store,
			queryMode: 'local',
			tpl: Ext.create('Ext.XTemplate',
				'<tpl for=".">',
					'<div class="x-boundlist-item">{NAMA_SATUAN}</div>',
				'</tpl>'
			),
			displayTpl: Ext.create('Ext.XTemplate',
				'<tpl for=".">',
					'{NAMA_SATUAN}',
				'</tpl>'
			),
			valueField: 'NAMA_SATUAN',
			displayField: 'NAMA_SATUAN',
			readOnly: true
		});
		var HARGA_SATUAN_field = Ext.create('Ext.form.field.Number', {
			name: 'HARGA_SATUAN', /* column name of table */
			fieldLabel: 'HARGA_SATUAN',
			readOnly: true
		});
		var HARGA_TOTAL_field = Ext.create('Ext.form.field.Number', {
			name: 'HARGA_TOTAL', /* column name of table */
			fieldLabel: 'HARGA_TOTAL',
			readOnly: true
		});
		var QUANTITY_field = Ext.create('Ext.form.field.Number', {
			name: 'QUANTITY', /* column name of table */
			fieldLabel: 'QUANTITY',
			minValue: 0,
			enableKeyEvents: true,
			listeners: {
				'keyup': function(editor, e){
					var harga = HARGA_SATUAN_field.getValue();
					var total = editor.value * harga;
					HARGA_TOTAL_field.setValue(total);
				}
			}
		});
		var NOTE_field = Ext.create('Ext.form.field.Text', {
			name: 'NOTE', /* column name of table */
			fieldLabel: 'NOTE',
			maxLength: 50 /* length of column name */
		});	
		var ID_field = Ext.create('Ext.form.field.Hidden', {
			name: 'ID', /* column name of table */
			fieldLabel: 'ID',
			readOnly: true
		});	
        Ext.apply(this, {
            fieldDefaults: {
                labelAlign: 'right',
                labelWidth: 120,
                msgTarget: 'qtip',
				anchor: '100%'
            },
			defaultType: 'textfield',
            items: [NOMOR_DO_field,TGL_DETAIL_DO_field,NOMOR_PO_field,KODE_BARANG_field,NAMA_BARANG_field,BATCH_NO_field,{
					xtype: 'fieldcontainer',
					layout: 'hbox',
					items: [QUANTITY_field,{
						xtype: 'splitter'
					},SISABARANG_field,{
						xtype: 'splitter'
					},SISAPOCDET_field]
				},SATUAN_field,HARGA_SATUAN_field,HARGA_TOTAL_field,NOTE_field,ID_field],
			
	        buttons: [{
                iconCls: 'icon-save',
                itemId: 'save',
                text: 'Save',
                disabled: true,
                action: 'save'
            }, {
                iconCls: 'icon-add',
				itemId: 'create',
                text: 'Create',
                action: 'create'
            }, {
                iconCls: 'icon-reset',
                text: 'Cancel',
                action: 'cancel'
            }]
        });
        
        this.callParent();
    }	
});